//
//  YYXHorizontalLinkViewController.m
//  33-严雅兴
//
//  Created by YanYanxing on 2017/2/23.
//  Copyright © 2017年 yanyaxing. All rights reserved.
//

#import "YYXHorizontalLinkViewController.h"
#import "YYXCategoryView.h"
#import "Masonry.h"

#define NAV_STATUES_HEIGHT 64
#define TOPView_HEIGHT 100

@interface YYXHorizontalLinkViewController ()<UIScrollViewDelegate>

@property (weak,nonatomic)UIView *topView;

@property (weak,nonatomic)YYXCategoryView *categoryView;

@property (weak,nonatomic)UIScrollView *scrollView;

@end

@implementation YYXHorizontalLinkViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self setupUI];
}


#pragma mark - scrollView的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //取出当前滚动的距离
    CGFloat offsetX = scrollView.contentOffset.x / 3; //得到上面小黄条应该滚动的距离
    
    //把这个滚动的距离，设置给小黄条的距离
    _categoryView.offsetX = offsetX;
}


//即将显示出来时，导航条一定有！
//-(void)viewWillAppear:(BOOL)animated{
//    
//    self.navigationController.navigationBar.alpha = 0;
//}



#pragma mark - 拖拽手势自动调用的方法
-(void)scaleTopView:(UIPanGestureRecognizer *)sender{
    
    //获取当前拖拽的位移值
    CGPoint offset = [sender translationInView:sender.view];
    //复位一次
    [sender setTranslation:CGPointZero inView:sender.view];
    
    CGFloat height = offset.y;
    
    if(height < NAV_STATUES_HEIGHT){
        
        return;
    }

    
    //可移动的高度
    CGFloat offsetHeight = NAV_STATUES_HEIGHT;
    
    //取出之前的alpha
    CGFloat oldAlpha = self.navigationController.navigationBar.alpha;
    
    //用当前移动的高度
    CGFloat alpha =  oldAlpha - offset.y * 1 / offsetHeight;
    
    //设置给导航条
    self.navigationController.navigationBar.alpha = alpha;
    
}

#pragma 改变页
-(void)changePage:(YYXCategoryView *)sender{
    [_scrollView setContentOffset:CGPointMake(sender.tag * _scrollView.bounds.size.width, 0) animated:YES];
}







#pragma mark - 初始化界面
-(void)setupUI{
    
    //头部视图
    UIView *topV = [UIView new];
    topV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topV];
    
    //做布局
    [topV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(TOPView_HEIGHT);
        make.left.right.top.equalTo(self.view);
    }];
    
    //记录头部视图
    _topView = topV;
    
    //MARK: 中间三个按钮视图
    YYXCategoryView *middleView = [YYXCategoryView new];
    //    middleView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:middleView];
    
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(topV.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(37);
    }];
    
    //自己写的UIController，事件统一用ValueChanged
    [middleView addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    
    //记录中间视图
    _categoryView = middleView;
    
    
    //MARK:底部scrollView
    UIScrollView *sv = [[UIScrollView alloc] init];
    sv.backgroundColor = [UIColor redColor];
    [self.view addSubview:sv];
    
    [sv mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(middleView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    //给scrollView添加三个控制器的根视图
    NSArray *classNameList = @[ @"YYXOneViewController",@"YYXTwoViewController",@"YYXThreeViewController" ];
    
    //准备保存所有根视图的数组
    NSMutableArray<UIView *> *viewList = [NSMutableArray array];
    for (NSInteger i = 0; i < classNameList.count; i++) {
        
        Class cls = NSClassFromString(classNameList[i]);
        
        UIViewController *vc = [[cls alloc] init];
        
        //把当前控制器的根视图加入到scrollView里面
        [sv addSubview:vc.view];
        
        //添加到数组
        [viewList addObject:vc.view];
    }
    
    //开启分页
    sv.pagingEnabled = YES;
    
    //记录scrollView
    _scrollView = sv;
    
    //设置scrollView的代理对象
    sv.delegate = self;
    
    //设置scrollView的子视图约束
    [viewList mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    [viewList mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(sv);
        
        make.size.equalTo(sv);//每个子视图有个确定的宽和高，就能自动帮我们算出contentSize
    }];
    
    
    //添加拖拽手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(scaleTopView:)];
    
    //把手势添加到根视图
    [self.view addGestureRecognizer:pan];
}

@end
